<?php
/**
 * Created by PhpStorm.
 * User: hetu
 * Date: 2018/5/10
 * Time: 9:52
 */

use \yii\helpers\Url;
use \yii\helpers\Html;
use common\service\ConstatService;
use common\models\AuthItem;
use backend\service\StoreService ;

$role = AuthItem::find()->andWhere(['type'=>1])->orderBy("created_at asc")->asArray()->all();
$role_arr = [''=>'请选择'];
foreach($role as $value){
    $role_arr[$value['name']] = $value['name'];
}
$status = [ConstatService::ADMIN_USER_STATUS_ENABLE=>'启用', ConstatService::ADMIN_USER_STATUS_DISABLE=>'禁用'] ;
?>
<form id="add-user-form" class="form-horizontal" method="post" enctype="multipart/form-data">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">添加用户</h4>
    </div>
    <div style="border-bottom: 1px solid #e5e5e5"></div>
    <div class="modal-body">
        <div class="box-body">
            <div class="form-group">
                <label class="col-sm-4 control-label">用户姓名：<span style="color:#FF0000;">*</span></label>
                <div class="input-group col-sm-5  required" style="padding-right: 15px;padding-left: 15px;">
                    <input class="form-control" type="text" id="RegisterForm-username" name="RegisterForm[username]" maxlength="20">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label">手机号码：<span style="color:#FF0000;">*</span></label>
                <div class="input-group col-sm-5  required" style="padding-right: 15px;padding-left: 15px;">
                    <input class="form-control" type="text" id="RegisterForm-phone" name="RegisterForm[phone]" maxlength="11" placeholder="必须填入11位手机号，用手机号登录系统操作">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label">密码：<span style="color:#FF0000;">*</span></label>
                <div class="input-group col-sm-5  required" style="padding-right: 15px;padding-left: 15px;">
                    <input class="form-control" type="password" id="RegisterForm-password" name="RegisterForm[password]" maxlength="50" placeholder="请填入6-20位密码">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-4 control-label">用户头像：</label>
                <div class="input-group col-sm-5  required" style="padding-right: 15px;padding-left: 15px;">
                    <input class="form-control" type="file" id="RegisterForm-avatar" name="RegisterForm[avatar]" accept="image/*">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-4 control-label">角色：<span style="color:#FF0000;">*</span></label>
                <div class="input-group col-sm-5 field-registerform-phone required" style="padding-right: 15px;padding-left: 15px;">
                    <select name="RegisterForm[role]" class="form-control">
                        <?php foreach ($role_arr as $key=>$role):?>
                            <option value="<?= $key ?>" ><?= $role ?></option>
                        <?php endforeach ?>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" >状态：</label>
                <div class="input-group col-sm-5 field-registerform-phone required" style="padding-right: 15px;padding-left: 15px;">
                    <select name="RegisterForm[status]" class="form-control">
                        <?php foreach ($status as $key=>$item):?>
                            <option value="<?= $key ?>"><?= $item ?></option>
                        <?php endforeach ?>
                    </select>
                </div>
            </div>
        </div>
    </div>
    <div class="modal-footer">
        <?= Html::hiddenInput('_csrf-backend',\Yii::$app->getRequest()->csrfToken) ?>
        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
        <button type="button" class="btn btn-primary" id="add-user-ajax-submit">保存</button>
    </div>
</form>
<script>
    $('#add-user-ajax-submit').click(function () {
        var data = new FormData(document.getElementById("add-user-form"));//表单id
        var button = $(this) ;
        var buttonText = $(this).text();
        var url = '<?= Url::toRoute('add-user') ?>' ;
        $.ajax({
            type: "POST",
            url: url,
            data: data,
            dataType: "json",
            cache: false,//上传文件无需缓存
            processData: false,//用于对data参数进行序列化处理 这里必须false
            contentType: false, //必须
            beforeSend: function () {
                showMask();
                button.text(buttonText + '中');
                button.attr('disabled', 'disabled');
            },
            success: function(data){
                button.text(buttonText);
                button.removeAttr('disabled');
                if (typeof(data.code) != "undefined" && data.code == 200) {
                    bootboxAlert(data.msg);
                    if (data.url) {
                        setTimeout(function(){window.location.href = data.url;},1000);
                    }
                    if(typeof callback === "function") {
                        callback(data.data);
                    }
                } else if (typeof(data.code) != "undefined") {
                    bootboxAlert(data.msg);
                }
            },
            error: function (data) {
                if (data.status !=  302) {
                    bootboxAlert(data.responseText);
                }
                button.text(buttonText);
                button.removeAttr('disabled');
            },
            complete:function(){
                hideMask() ;
            }
        });
    });
</script>